package net.office.service.impl;

import net.office.bean.Memorandum;
import net.office.bean.MemorandumExample;
import net.office.mapper.MemorandumMapper;
import net.office.service.MemorandumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemorandumServiceImpl implements MemorandumService {

    @Autowired
    private MemorandumMapper memorandumMapper;

    @Override
    public List<Memorandum> findAllByStaffId(int staffId) {
        MemorandumExample example = new MemorandumExample();
        example.createCriteria().andStaffIdEqualTo(staffId);

        return memorandumMapper.selectByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer memorId) {

        return memorandumMapper.deleteByPrimaryKey(memorId);
    }

    @Override
    public List<Memorandum> findByMemorandum(Memorandum memorandum) {
        MemorandumExample example = new MemorandumExample();
        MemorandumExample.Criteria criteria = example.createCriteria();

        if(memorandum.getMemorId() != null){
            criteria.andMemorIdEqualTo(memorandum.getMemorId());
        }
        if(memorandum.getStaffId() != null){
            criteria.andStaffIdEqualTo(memorandum.getStaffId());
        }
        if(memorandum.getAssignDate() != null){
            criteria.andAssignDateEqualTo(memorandum.getAssignDate());
        }
        if(memorandum.getAssignProjectName() != null
                && memorandum.getAssignProjectName().trim().length() != 0){
            criteria.andAssignProjectNameLike("%" + memorandum.getAssignProjectName() + "%");
        }
        if(memorandum.getAssignStaffId() != null){
            criteria.andAssignStaffIdEqualTo(memorandum.getAssignStaffId());
        }
        if(memorandum.getAssignStaffName() != null
                && memorandum.getAssignStaffName().trim().length() != 0){
            criteria.andAssignStaffNameLike("%" + memorandum.getAssignStaffName() + "%");
        }
        if(memorandum.getWorkStartDate() != null){
            criteria.andWorkStartDateEqualTo(memorandum.getWorkStartDate());
        }
        if(memorandum.getWorkPlanDate() != null){
            criteria.andWorkPlanDateEqualTo(memorandum.getWorkPlanDate());
        }
        if(memorandum.getWorkFinishDate() != null){
            criteria.andWorkFinishDateEqualTo(memorandum.getWorkFinishDate());
        }
        if(memorandum.getWorkStatus() != null){
            criteria.andWorkStatusEqualTo(memorandum.getWorkStatus());
        }
        if(memorandum.getWorkOverdueReason() != null
                && memorandum.getWorkOverdueReason().trim().length() != 0){
            criteria.andWorkOverdueReasonLike("%" + memorandum.getWorkOverdueReason() + "%");
        }
        if(memorandum.getWorkDealResult() != null
                && memorandum.getWorkDealResult().trim().length() != 0){
            criteria.andWorkDealResultLike("%" + memorandum.getWorkDealResult() + "%");
        }
        if(memorandum.getWorkContent() != null
                && memorandum.getWorkContent().trim().length() != 0){
            criteria.andWorkContentLike("%" + memorandum.getWorkContent() + "%");
        }
        example.or(criteria);

        return memorandumMapper.selectByExample(example);
    }

    @Override
    public int insert(Memorandum memorandum) {
        return memorandumMapper.insertSelective(memorandum);
    }

    @Override
    public Memorandum findOneByMemorId(Integer memorId) {
        return memorandumMapper.selectByPrimaryKey(memorId);
    }

    @Override
    public int updateByPrimaryKey(Memorandum memorandum) {
        return memorandumMapper.updateByPrimaryKey(memorandum);
    }
}
